package com.yozinga.database;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class YozingaContentProvider extends ContentProvider{

	
	private YozingaDatabase databaseHelper;
	private UriMatcher uriMatcher = buildMatcher();
	private static final int FAVOURITE = 100;
	
	
	private static UriMatcher buildMatcher() {
		UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(yozingaDataBaseManager.CONTENT_AUTHORITY, yozingaDataBaseManager.Favourites.PATH_FAVOURITE, FAVOURITE);
		return matcher;
	}
	
	@Override
	public boolean onCreate() {
		databaseHelper = new YozingaDatabase(getContext());
		return true;

	}

	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db;
		switch (uriMatcher.match(uri)) {
		case FAVOURITE:
		default:
			db = databaseHelper.getWritableDatabase();
			String table = getTableForUri(uri);
			int count = db.delete(table, selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			return count;
		}
	}
	

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		final SQLiteDatabase db = databaseHelper.getWritableDatabase();
		String table = getTableForUri(uri);

		long id = db.insertOrThrow(table, null, values);

		if (id > 0) {
			Uri itemUri = ContentUris.withAppendedId(uri, id);
			getContext().getContentResolver().notifyChange(itemUri, null);
			return itemUri;
		} else {
			throw new SQLException("Failed to insert row into " + table);
		}
	}

	

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		final SQLiteDatabase db = databaseHelper.getReadableDatabase();
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		queryBuilder.setTables(getTableForUri(uri));
		Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
	
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		String table = getTableForUri(uri);

		int count = db.update(table, values, selection, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
	

	private String getTableForUri(Uri uri) {
		switch (uriMatcher.match(uri)) {
		case FAVOURITE:
			return yozingaDataBaseManager.Favourites.TABLE_FAVOURITE;
		default:
			throw new UnsupportedOperationException("Unknown uri: " + uri);
		}
	}

}
